package com.punai.assetlibrary.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.punai.assetlibrary.entiry.dtos.ZjfzckDto;
import com.punai.assetlibrary.service.ZJFZckService;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.page.TableDataInfo;
import com.punai.common.utils.StringUtil;
import com.punai.evaluationSystem.costDataEvalute.domain.EvaluationCbInfo;
import com.punai.evaluationSystem.costDataEvalute.domain.EvaluationDataPrice;
import com.punai.evaluationSystem.costDataEvalute.domain.EvaluationResults;
import com.punai.externalccb.domain.EcoCbfeeitem;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping("/assetslibrary/zjfzck")
public class ZJFZckController extends BaseController {

    @Autowired
    private ZJFZckService zjfZckService;


    @PostMapping("initTableData")
    public TableDataInfo initDataTable(@RequestBody ZjfzckDto zjfzckDto) {
        String[] dateRange = zjfzckDto.getDateRange();
        if (dateRange != null && dateRange.length >= 2) {
            if (StringUtil.isNotNullOrEmpty(dateRange[0])) {
                zjfzckDto.setTimeStart(dateRange[0]);
            }

            if (StringUtil.isNotNullOrEmpty(dateRange[1])) {
                zjfzckDto.setTimeEnd(dateRange[1]);
            }
        }

        if ("全部".equals(zjfzckDto.getProjectDept())) {
            zjfzckDto.setProjectDept(null);
        }

        String address = StrUtil.join("", zjfzckDto.getAddressList());
        if ("全部".equals(address)) {
            zjfzckDto.setAddress(null);
        } else {
            zjfzckDto.setAddress(address);
        }

        if ("全部".equals(zjfzckDto.getPjjgsj())) {
            zjfzckDto.setPjjgsj(null);
        }

        List<EvaluationDataPrice> data = zjfZckService.initTableData(zjfzckDto);
        for (EvaluationDataPrice evaluationDataPrice : data) {
            String pid = evaluationDataPrice.getPid();
            EvaluationResults results = zjfZckService.findByPid(pid);
            evaluationDataPrice.setJgsl(results.getBtjgsl());
        }
        return getDataTable(data);
    }


    @PostMapping("/main1")
    public AjaxResult main1(@RequestBody ZjfzckDto zjfzckDto) {
        String[] dateRange = zjfzckDto.getDateRange();
        if (dateRange != null && dateRange.length >= 2) {
            if (StringUtil.isNotNullOrEmpty(dateRange[0])) {
                zjfzckDto.setTimeStart(dateRange[0]);
            }

            if (StringUtil.isNotNullOrEmpty(dateRange[1])) {
                zjfzckDto.setTimeEnd(dateRange[1]);
            }
        }

        if ("全部".equals(zjfzckDto.getProjectDept())) {
            zjfzckDto.setProjectDept(null);
        }

        String address = StrUtil.join("", zjfzckDto.getAddressList());
        if ("全部".equals(address)) {
            zjfzckDto.setAddress(null);
        } else {
            zjfzckDto.setAddress(address);
        }

        if ("全部".equals(zjfzckDto.getPjjgsj())) {
            zjfzckDto.setPjjgsj(null);
        }

        String code = zjfzckDto.getListCode();
        Map<String, Object> map = new HashMap<>();
        map.put("code", code);
        map.put("pjjsj", zjfzckDto.getPjjgsj());
        map.put("xmgs", zjfzckDto.getProjectDept());
        map.put("region", zjfzckDto.getAddress());
        String dtype = zjfzckDto.getDtype1();
        if ("1".equals(dtype)) {
            zjfzckDto.setType("01");
        } else if ("2".equals(dtype)) {
            zjfzckDto.setType("01");
        } else if ("3".equals(dtype)) {
            zjfzckDto.setType("02");
        } else if ("4".equals(dtype)) {
            zjfzckDto.setType("02");
        } else if ("5".equals(dtype)) {
            zjfzckDto.setType("02");
        } else if ("6".equals(dtype)) {
            zjfzckDto.setType("02");
        }
        List<EvaluationDataPrice> list = zjfZckService.findAllEvaluationDataPrice(zjfzckDto);
        Map<String, List<EvaluationDataPrice>> map1 = list.stream().collect(Collectors.groupingBy(EvaluationDataPrice::getPjjgsj, Collectors.toList()));

        List<EvaluationCbInfo> userGS = zjfZckService.findAllEvaluationInfo(map1);
        userGS.stream().forEach(v -> {
            if (v.getProjectDept() == null) {
                v.setProjectDept("");
            }
            if (v.getAddress() == null) {
                v.setAddress("");
            }
        });
        Map<String, Object> result = new HashMap<>();
        List<String> x = new ArrayList<>();
        List<String> y = new ArrayList<>();

        Map<String, List<EvaluationCbInfo>> listMap = userGS.stream().filter(v -> v.getPjjgsj() != null).collect(Collectors.groupingBy(EvaluationCbInfo::getPjjgsj, Collectors.toList()));
        for (Map.Entry<String, List<EvaluationCbInfo>> entry : listMap.entrySet()) {
            x.add(entry.getKey());
        }
        switch (dtype) {
            case "1":
                for (EvaluationDataPrice price : list) {
                    BigDecimal userPrice = price.getUserPrice();
                    Integer jgsl = price.getJgsl();
                    if (userPrice != null && jgsl != null) {
                        price.setFeeitemprice(userPrice.multiply(BigDecimal.valueOf(jgsl)));
                    }
                }
                Map<String, List<EvaluationDataPrice>> mapR = list.stream().collect(Collectors.groupingBy(EvaluationDataPrice::getPjjgsj, Collectors.toList()));

                for (Map.Entry<String, List<EvaluationDataPrice>> entry : mapR.entrySet()) {
                    BigDecimal ry = entry.getValue().stream().map(EvaluationDataPrice::getFeeitemprice).reduce(BigDecimal.ZERO, BigDecimal::add);
                    y.add(ry.setScale(3, BigDecimal.ROUND_HALF_UP).toPlainString());
                }

                result.put("labels", x);
                result.put("values", y);

                return AjaxResult.success(result);
            case "2":
                for (String s : x) {
                    List<EvaluationCbInfo> dataCleanInfos = listMap.get(s);
                    if (CollUtil.isNotEmpty(dataCleanInfos)) {
                        y.add(String.valueOf(dataCleanInfos.size()));
                    }
                }

                result.put("labels", x);
                result.put("values", y);

                return AjaxResult.success(result);

            case "3":
                for (String s : x) {
                    List<EvaluationCbInfo> dataCleanInfos = listMap.get(s);
                    List<EcoCbfeeitem> cbFeeItems = new ArrayList<>();

                    if (CollUtil.isNotEmpty(dataCleanInfos)) {
                        for (EvaluationCbInfo cleanInfo : dataCleanInfos) {
                            if (StringUtils.isNotEmpty(cleanInfo.getDataJson())) {
                                EcoCbfeeitem cbFeeItem = JSON.parseObject(cleanInfo.getDataJson(), EcoCbfeeitem.class);
                                if (cbFeeItem != null) {
                                    cbFeeItems.add(cbFeeItem);
                                }
                            }
                        }
                    }
                    if (CollUtil.isNotEmpty(cbFeeItems)) {

                        BigDecimal ystotal = cbFeeItems.stream().filter(e -> e.getYsprice() != null).map(EcoCbfeeitem::getYsprice).reduce(BigDecimal.ZERO, BigDecimal::add);

                        y.add(ystotal.toPlainString());
                    }
                }

                result.put("labels", x);
                result.put("values", y);

                return AjaxResult.success(result);

            case "4":
                for (String s : x) {
                    List<EvaluationCbInfo> dataCleanInfos = listMap.get(s);
                    List<EcoCbfeeitem> cbFeeItems = new ArrayList<>();

                    if (CollUtil.isNotEmpty(dataCleanInfos)) {
                        for (EvaluationCbInfo cleanInfo : dataCleanInfos) {
                            if (StringUtils.isNotEmpty(cleanInfo.getDataJson())) {
                                EcoCbfeeitem cbFeeItem = JSON.parseObject(cleanInfo.getDataJson(), EcoCbfeeitem.class);
                                if (cbFeeItem != null) {
                                    cbFeeItems.add(cbFeeItem);
                                }
                            }
                        }
                    }

                    if (CollUtil.isNotEmpty(cbFeeItems)) {

                        List<EcoCbfeeitem> taxList = cbFeeItems.stream().filter(e -> e.getTargettaxprice() != null).collect(Collectors.toList());

                        BigDecimal tartotal = taxList.stream().map(EcoCbfeeitem::getTargettaxprice).reduce(BigDecimal.ZERO, BigDecimal::add);

                        y.add(tartotal.toPlainString());
                    }
                }

                result.put("labels", x);
                result.put("values", y);

                return AjaxResult.success(result);

            case "5":
                for (String s : x) {
                    List<EvaluationCbInfo> dataCleanInfos = listMap.get(s);
                    List<EcoCbfeeitem> cbFeeItems = new ArrayList<>();
                    if (CollUtil.isNotEmpty(dataCleanInfos)) {
                        for (EvaluationCbInfo cleanInfo : dataCleanInfos) {
                            if (StringUtils.isNotEmpty(cleanInfo.getDataJson())) {
                                EcoCbfeeitem cbFeeItem = JSON.parseObject(cleanInfo.getDataJson(), EcoCbfeeitem.class);
                                if (cbFeeItem != null) {
                                    cbFeeItems.add(cbFeeItem);
                                }
                            }
                        }
                    }

                    if (CollUtil.isNotEmpty(cbFeeItems)) {

                        List<EcoCbfeeitem> yspriceList = cbFeeItems.stream().filter(e -> e.getYsprice() != null).collect(Collectors.toList());

                        BigDecimal ystotal = yspriceList.stream().map(EcoCbfeeitem::getYsprice).reduce(BigDecimal.ZERO, BigDecimal::add);

                        List<EcoCbfeeitem> taxList = cbFeeItems.stream().filter(e -> e.getTargettaxprice() != null).collect(Collectors.toList());

                        BigDecimal tartotal = taxList.stream().map(EcoCbfeeitem::getTargettaxprice).reduce(BigDecimal.ZERO, BigDecimal::add);

                        BigDecimal r = ystotal.subtract(tartotal).setScale(3, BigDecimal.ROUND_HALF_UP);

                        y.add(r.toPlainString());
                    }
                }

                result.put("labels", x);
                result.put("values", y);

                return AjaxResult.success(result);

            case "6":
                for (String s : x) {
                    List<EvaluationCbInfo> dataCleanInfos = listMap.get(s);
                    List<EcoCbfeeitem> cbFeeItems = new ArrayList<>();
                    if (CollUtil.isNotEmpty(dataCleanInfos)) {
                        for (EvaluationCbInfo cleanInfo : dataCleanInfos) {
                            if (StringUtils.isNotEmpty(cleanInfo.getDataJson())) {
                                EcoCbfeeitem cbFeeItem = JSON.parseObject(cleanInfo.getDataJson(), EcoCbfeeitem.class);
                                if (cbFeeItem != null) {
                                    cbFeeItems.add(cbFeeItem);
                                }
                            }
                        }
                    }

                    if (CollUtil.isNotEmpty(cbFeeItems)) {

                        List<EcoCbfeeitem> yspriceList = cbFeeItems.stream().filter(e -> e.getYsprice() != null).collect(Collectors.toList());

                        BigDecimal ystotal = yspriceList.stream().map(EcoCbfeeitem::getYsprice).reduce(BigDecimal.ZERO, BigDecimal::add);

                        List<EcoCbfeeitem> taxList = cbFeeItems.stream().filter(e -> e.getTargettaxprice() != null).collect(Collectors.toList());

                        BigDecimal tartotal = taxList.stream().map(EcoCbfeeitem::getTargettaxprice).reduce(BigDecimal.ZERO, BigDecimal::add);

                        BigDecimal s1 = ystotal.subtract(tartotal);
                        BigDecimal r = BigDecimal.ZERO;
                        if (BigDecimal.ZERO.compareTo(ystotal) != 0) {
                            r = s1.divide(ystotal, BigDecimal.ROUND_HALF_UP).setScale(3, BigDecimal.ROUND_HALF_UP);
                        }

                        y.add(r.toPlainString());
                    }

                }

                result.put("labels", x);
                result.put("values", y);

                return AjaxResult.success(result);

            default:
                break;
        }
        result.put("labels", x);
        result.put("values", y);

        return AjaxResult.success(result);

    }

    @PostMapping("/main2")
    public AjaxResult main2(@RequestBody ZjfzckDto zjfzckDto) {
        String[] dateRange = zjfzckDto.getDateRange();
        if (dateRange != null && dateRange.length >= 2) {
            if (StringUtil.isNotNullOrEmpty(dateRange[0])) {
                zjfzckDto.setTimeStart(dateRange[0]);
            }

            if (StringUtil.isNotNullOrEmpty(dateRange[1])) {
                zjfzckDto.setTimeEnd(dateRange[1]);
            }
        }

        if ("全部".equals(zjfzckDto.getProjectDept())) {
            zjfzckDto.setProjectDept(null);
        }

        String address = StrUtil.join("", zjfzckDto.getAddressList());
        if ("全部".equals(address)) {
            zjfzckDto.setAddress(null);
        } else {
            zjfzckDto.setAddress(address);
        }

        if ("全部".equals(zjfzckDto.getPjjgsj())) {
            zjfzckDto.setPjjgsj(null);
        }


        String code = zjfzckDto.getListCode();
        Map<String, Object> map = new HashMap<>();
        map.put("code", code);
        map.put("pjjsj", zjfzckDto.getPjjgsj());
        map.put("xmgs", zjfzckDto.getProjectDept());
        map.put("region", zjfzckDto.getAddress());
        String dtype = zjfzckDto.getDtype2();
        if ("1".equals(dtype)) {
            zjfzckDto.setType("01");
        } else if ("2".equals(dtype)) {
            zjfzckDto.setType("01");
        } else if ("3".equals(dtype)) {
            zjfzckDto.setType("02");
        } else if ("4".equals(dtype)) {
            zjfzckDto.setType("02");
        } else if ("5".equals(dtype)) {
            zjfzckDto.setType("02");
        } else if ("6".equals(dtype)) {
            zjfzckDto.setType("02");
        }
        List<EvaluationDataPrice> list = zjfZckService.findAllEvaluationDataPrice(zjfzckDto);


        Map<String, List<EvaluationDataPrice>> map1 = list.stream().collect(Collectors.groupingBy(EvaluationDataPrice::getPjjgsj, Collectors.toList()));
        Map<String, List<EvaluationDataPrice>> mapIdList = list.stream().collect(Collectors.groupingBy(EvaluationDataPrice::getId, Collectors.toList()));

        List<EvaluationCbInfo> userGS = zjfZckService.findAllEvaluationInfo(map1);
        userGS.stream().forEach(v -> {
            if (v.getProjectDept() == null) {
                v.setProjectDept("");
            }
            if (v.getAddress() == null) {
                v.setAddress("");
            }
        });

        Map<String, List<EvaluationCbInfo>> userQC = userGS.stream().collect(Collectors.groupingBy(EvaluationCbInfo::getPjjgsj, Collectors.toList()));

        ArrayList<EvaluationCbInfo> listIM = new ArrayList<>();

        for (Map.Entry<String, List<EvaluationCbInfo>> entry : userQC.entrySet()) {
            for (EvaluationCbInfo evaluateInfo : entry.getValue()) {
                List<EvaluationDataPrice> prices = mapIdList.get(evaluateInfo.getPid());
                if (CollUtil.isNotEmpty(prices)) {
                    BigDecimal userPrice = prices.get(0).getUserPrice();
                    if (userPrice != null) {
                        evaluateInfo.setPrice(userPrice);
                    }
                }
                listIM.add(evaluateInfo);
            }
        }

        Map<String, Object> result = new HashMap<>();
        List<String> x = new ArrayList<>();
        List<Map<String, Object>> y = new ArrayList<>();

        Map<String, List<EvaluationCbInfo>> listMap = listIM.stream().filter(v -> v.getPjjgsj() != null).collect(Collectors.groupingBy(EvaluationCbInfo::getPjjgsj, Collectors.toList()));
        for (Map.Entry<String, List<EvaluationCbInfo>> entry : listMap.entrySet()) {
            x.add(entry.getKey());
        }
        Map<String, List<EvaluationCbInfo>> gs = listIM.stream().filter(v -> v.getProjectDept() != null).collect(Collectors.groupingBy(EvaluationCbInfo::getProjectDept, Collectors.toList()));
        switch (dtype) {
            case "1":
                for (Map.Entry<String, List<EvaluationCbInfo>> entry : gs.entrySet()) {
                    Map<String, Object> m = new HashMap<>();
                    m.put("name", entry.getKey());
                    m.put("type", "line");
                    ArrayList<String> d = new ArrayList<>();
                    for (String s : x) {
                        Map<String, List<EvaluationCbInfo>> map2 = entry.getValue().stream().collect(Collectors.groupingBy(EvaluationCbInfo::getPjjgsj, Collectors.toList()));
                        List<EvaluationCbInfo> infoList = map2.get(s);

                        if (CollUtil.isNotEmpty(infoList)) {
                            BigDecimal value = infoList.stream().map(EvaluationCbInfo::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(3, BigDecimal.ROUND_HALF_UP);
                            d.add(value.toPlainString());
                        } else {
                            d.add("");
                        }
                    }

                    m.put("data", d);
                    y.add(m);
                }
                result.put("labels", x);
                result.put("values", y);

                return AjaxResult.success(result);

            case "2":
                for (Map.Entry<String, List<EvaluationCbInfo>> entry : gs.entrySet()) {
                    Map<String, Object> m = new HashMap<>();
                    m.put("name", entry.getKey());
                    m.put("type", "line");
                    ArrayList<String> d = new ArrayList<>();
                    for (String s : x) {
                        Map<String, List<EvaluationCbInfo>> map2 = entry.getValue().stream().collect(Collectors.groupingBy(EvaluationCbInfo::getPjjgsj, Collectors.toList()));
                        List<EvaluationCbInfo> infoList = map2.get(s);
                        if (CollUtil.isNotEmpty(infoList)) {
                            d.add(String.valueOf(infoList.size()));
                        } else {
                            d.add("-");
                        }
                    }

                    m.put("data", d);
                    y.add(m);
                }

                result.put("labels", x);
                result.put("values", y);

                return AjaxResult.success(result);

            case "3":
                for (Map.Entry<String, List<EvaluationCbInfo>> entry : gs.entrySet()) {
                    Map<String, Object> m = new HashMap<>();
                    m.put("name", entry.getKey());
                    m.put("type", "line");
                    ArrayList<String> d = new ArrayList<>();
                    for (String s : x) {
                        Map<String, List<EvaluationCbInfo>> map2 = entry.getValue().stream().collect(Collectors.groupingBy(EvaluationCbInfo::getPjjgsj, Collectors.toList()));
                        List<EvaluationCbInfo> infoList = map2.get(s);
                        List<EcoCbfeeitem> cbFeeItems = new ArrayList<>();

                        if (CollUtil.isNotEmpty(infoList)) {
                            for (EvaluationCbInfo cleanInfo : infoList) {
                                if (StringUtils.isNotEmpty(cleanInfo.getDataJson())) {
                                    EcoCbfeeitem cbFeeItem = JSON.parseObject(cleanInfo.getDataJson(), EcoCbfeeitem.class);
                                    if (cbFeeItem != null) {
                                        cbFeeItems.add(cbFeeItem);
                                    }
                                }
                            }
                        }

                        if (CollUtil.isNotEmpty(cbFeeItems)) {
                            List<EcoCbfeeitem> yspriceList = cbFeeItems.stream().filter(e -> e.getYsprice() != null).collect(Collectors.toList());
                            BigDecimal ystotal = yspriceList.stream().map(EcoCbfeeitem::getYsprice).reduce(BigDecimal.ZERO, BigDecimal::add);
                            d.add(ystotal.toPlainString());
                        } else {
                            d.add("-");
                        }

                    }

                    m.put("data", d);
                    y.add(m);
                }

                result.put("labels", x);
                result.put("values", y);

                return AjaxResult.success(result);

            case "4":
                for (Map.Entry<String, List<EvaluationCbInfo>> entry : gs.entrySet()) {
                    Map<String, Object> m = new HashMap<>();
                    m.put("name", entry.getKey());
                    m.put("type", "line");
                    ArrayList<String> d = new ArrayList<>();
                    for (String s : x) {
                        Map<String, List<EvaluationCbInfo>> map2 = entry.getValue().stream().collect(Collectors.groupingBy(EvaluationCbInfo::getPjjgsj, Collectors.toList()));
                        List<EvaluationCbInfo> infoList = map2.get(s);
                        List<EcoCbfeeitem> cbFeeItems = new ArrayList<>();

                        if (CollUtil.isNotEmpty(infoList)) {
                            for (EvaluationCbInfo cleanInfo : infoList) {
                                if (StringUtils.isNotEmpty(cleanInfo.getDataJson())) {
                                    EcoCbfeeitem cbFeeItem = JSON.parseObject(cleanInfo.getDataJson(), EcoCbfeeitem.class);
                                    if (cbFeeItem != null) {
                                        cbFeeItems.add(cbFeeItem);
                                    }
                                }
                            }
                        }

                        if (CollUtil.isNotEmpty(cbFeeItems)) {

                            List<EcoCbfeeitem> taxList = cbFeeItems.stream().filter(e -> e.getTargettaxprice() != null).collect(Collectors.toList());

                            BigDecimal tartotal = taxList.stream().map(EcoCbfeeitem::getTargettaxprice).reduce(BigDecimal.ZERO, BigDecimal::add);

                            d.add(tartotal.toPlainString());
                        } else {
                            d.add("-");
                        }

                    }

                    m.put("data", d);
                    y.add(m);
                }

                result.put("labels", x);
                result.put("values", y);

                return AjaxResult.success(result);

            case "5":
                for (Map.Entry<String, List<EvaluationCbInfo>> entry : gs.entrySet()) {
                    Map<String, Object> m = new HashMap<>();
                    m.put("name", entry.getKey());
                    m.put("type", "line");
                    ArrayList<String> d = new ArrayList<>();
                    for (String s : x) {
                        Map<String, List<EvaluationCbInfo>> map2 = entry.getValue().stream().collect(Collectors.groupingBy(EvaluationCbInfo::getPjjgsj, Collectors.toList()));
                        List<EvaluationCbInfo> infoList = map2.get(s);
                        List<EcoCbfeeitem> cbFeeItems = new ArrayList<>();

                        if (CollUtil.isNotEmpty(infoList)) {
                            for (EvaluationCbInfo cleanInfo : infoList) {
                                if (StringUtils.isNotEmpty(cleanInfo.getDataJson())) {
                                    EcoCbfeeitem cbFeeItem = JSON.parseObject(cleanInfo.getDataJson(), EcoCbfeeitem.class);
                                    if (cbFeeItem != null) {
                                        cbFeeItems.add(cbFeeItem);
                                    }
                                }
                            }
                        }

                        if (CollUtil.isNotEmpty(cbFeeItems)) {

                            List<EcoCbfeeitem> yspriceList = cbFeeItems.stream().filter(e -> e.getYsprice() != null).collect(Collectors.toList());

                            BigDecimal ystotal = yspriceList.stream().map(EcoCbfeeitem::getYsprice).reduce(BigDecimal.ZERO, BigDecimal::add);

                            List<EcoCbfeeitem> taxList = cbFeeItems.stream().filter(e -> e.getTargettaxprice() != null).collect(Collectors.toList());

                            BigDecimal tartotal = taxList.stream().map(EcoCbfeeitem::getTargettaxprice).reduce(BigDecimal.ZERO, BigDecimal::add);

                            BigDecimal r = ystotal.subtract(tartotal).setScale(3, BigDecimal.ROUND_HALF_UP);

                            d.add(r.toPlainString());
                        } else {
                            d.add("-");
                        }
                    }
                    m.put("data", d);
                    y.add(m);
                }

                result.put("labels", x);
                result.put("values", y);

                return AjaxResult.success(result);

            case "6":
                for (Map.Entry<String, List<EvaluationCbInfo>> entry : gs.entrySet()) {
                    Map<String, Object> m = new HashMap<>();
                    m.put("name", entry.getKey());
                    m.put("type", "line");
                    ArrayList<String> d = new ArrayList<>();
                    for (String s : x) {
                        Map<String, List<EvaluationCbInfo>> map2 = entry.getValue().stream().collect(Collectors.groupingBy(EvaluationCbInfo::getPjjgsj, Collectors.toList()));
                        List<EvaluationCbInfo> infoList = map2.get(s);
                        List<EcoCbfeeitem> cbFeeItems = new ArrayList<>();

                        if (CollUtil.isNotEmpty(infoList)) {
                            for (EvaluationCbInfo cleanInfo : infoList) {
                                if (StringUtils.isNotEmpty(cleanInfo.getDataJson())) {
                                    EcoCbfeeitem cbFeeItem = JSON.parseObject(cleanInfo.getDataJson(), EcoCbfeeitem.class);
                                    if (cbFeeItem != null) {
                                        cbFeeItems.add(cbFeeItem);
                                    }
                                }
                            }
                        }

                        if (CollUtil.isNotEmpty(cbFeeItems)) {

                            List<EcoCbfeeitem> yspriceList = cbFeeItems.stream().filter(e -> e.getYsprice() != null).collect(Collectors.toList());
                            List<EcoCbfeeitem> taxList = cbFeeItems.stream().filter(e -> e.getTargettaxprice() != null).collect(Collectors.toList());

                            BigDecimal ystotal = yspriceList.stream().map(EcoCbfeeitem::getYsprice).reduce(BigDecimal.ZERO, BigDecimal::add);
                            BigDecimal tartotal = taxList.stream().map(EcoCbfeeitem::getTargettaxprice).reduce(BigDecimal.ZERO, BigDecimal::add);
                            String s1 = "0";
                            if (BigDecimal.ZERO.compareTo(ystotal) != 0) {
                                s1 = ystotal.subtract(tartotal).divide(ystotal, 3, BigDecimal.ROUND_HALF_UP).toPlainString();
                            }

                            d.add(s1);
                        } else {
                            d.add("-");
                        }
                    }
                    m.put("data", d);
                    y.add(m);
                }

                result.put("labels", x);
                result.put("values", y);

                return AjaxResult.success(result);

            default:
                break;
        }
        result.put("labels", x);
        result.put("values", y);

        return AjaxResult.success(result);

    }

    @PostMapping("/main3")
    public AjaxResult main3(@RequestBody ZjfzckDto zjfzckDto) {
        String[] dateRange = zjfzckDto.getDateRange();
        if (dateRange != null && dateRange.length >= 2) {
            if (StringUtil.isNotNullOrEmpty(dateRange[0])) {
                zjfzckDto.setTimeStart(dateRange[0]);
            }

            if (StringUtil.isNotNullOrEmpty(dateRange[1])) {
                zjfzckDto.setTimeEnd(dateRange[1]);
            }
        }

        if ("全部".equals(zjfzckDto.getProjectDept())) {
            zjfzckDto.setProjectDept(null);
        }

        String address = StrUtil.join("", zjfzckDto.getAddressList());
        if ("全部".equals(address)) {
            zjfzckDto.setAddress(null);
        } else {
            zjfzckDto.setAddress(address);
        }

        if ("全部".equals(zjfzckDto.getPjjgsj())) {
            zjfzckDto.setPjjgsj(null);
        }



        String code = zjfzckDto.getListCode();
        Map<String, Object> map = new HashMap<>();
        map.put("code", code);
        map.put("pjjsj", zjfzckDto.getPjjgsj());
        map.put("xmgs", zjfzckDto.getProjectDept());
        map.put("region", zjfzckDto.getAddress());
        String dtype = zjfzckDto.getDtype3();
        if ("1".equals(dtype)) {
            zjfzckDto.setType("01");
        } else if ("2".equals(dtype)) {
            zjfzckDto.setType("01");
        } else if ("3".equals(dtype)) {
            zjfzckDto.setType("02");
        } else if ("4".equals(dtype)) {
            zjfzckDto.setType("02");
        } else if ("5".equals(dtype)) {
            zjfzckDto.setType("02");
        } else if ("6".equals(dtype)) {
            zjfzckDto.setType("02");
        }
        List<EvaluationDataPrice> list = zjfZckService.findAllEvaluationDataPrice(zjfzckDto);

        Map<String, List<EvaluationDataPrice>> map1 = list.stream().collect(Collectors.groupingBy(EvaluationDataPrice::getPjjgsj, Collectors.toList()));
        Map<String, List<EvaluationDataPrice>> mapIdList = list.stream().collect(Collectors.groupingBy(EvaluationDataPrice::getId, Collectors.toList()));


        List<EvaluationCbInfo> userGS = zjfZckService.findAllEvaluationInfo(map1);
        userGS.stream().forEach(v -> {
            if (v.getProjectDept() == null) {
                v.setProjectDept("");
            }
            if (v.getAddress() == null) {
                v.setAddress("");
            }
        });

        Map<String, Object> result = new HashMap<>();
        List<String> x = new ArrayList<>();
        List<Map<String, Object>> y = new ArrayList<>();

        ArrayList<EvaluationCbInfo> listIM = new ArrayList<>();

        Map<String, List<EvaluationCbInfo>> userQC = userGS.stream().collect(Collectors.groupingBy(EvaluationCbInfo::getPjjgsj, Collectors.toList()));

        for (Map.Entry<String, List<EvaluationCbInfo>> entry : userQC.entrySet()) {
            for (EvaluationCbInfo evaluateInfo : entry.getValue()) {
                List<EvaluationDataPrice> prices = mapIdList.get(evaluateInfo.getPid());
                if (CollUtil.isNotEmpty(prices)) {
                    BigDecimal userPrice = prices.get(0).getUserPrice();
                    if (userPrice != null) {
                        evaluateInfo.setPrice(userPrice);
                    }
                }
                listIM.add(evaluateInfo);
            }
        }

        Map<String, List<EvaluationCbInfo>> listMap = listIM.stream().filter(v -> v.getPjjgsj() != null).collect(Collectors.groupingBy(EvaluationCbInfo::getPjjgsj, Collectors.toList()));
        for (Map.Entry<String, List<EvaluationCbInfo>> entry : listMap.entrySet()) {
            x.add(entry.getKey());
        }

        Map<String, List<EvaluationCbInfo>> area = userGS.stream().filter(v -> v.getAddress() != null).collect(Collectors.groupingBy(EvaluationCbInfo::getAddress, Collectors.toList()));


        Map<String, List<EvaluationCbInfo>> gs = listIM.stream().filter(v -> v.getAddress() != null).collect(Collectors.groupingBy(EvaluationCbInfo::getAddress, Collectors.toList()));

        switch (dtype) {
            case "1":
                for (Map.Entry<String, List<EvaluationCbInfo>> entry : gs.entrySet()) {
                    Map<String, Object> m = new HashMap<>();
                    m.put("name", entry.getKey());
                    m.put("type", "line");
                    ArrayList<String> d = new ArrayList<>();
                    Map<String, List<EvaluationCbInfo>> map2 = entry.getValue().stream().collect(Collectors.groupingBy(EvaluationCbInfo::getPjjgsj, Collectors.toList()));
                    for (String s : x) {
                        List<EvaluationCbInfo> infoList = map2.get(s);
                        if (CollUtil.isNotEmpty(infoList)) {
                            BigDecimal value = infoList.stream().map(EvaluationCbInfo::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(3, BigDecimal.ROUND_HALF_UP);
                            d.add(value.toPlainString());
                        } else {
                            d.add("");
                        }
                    }
                    m.put("data", d);
                    y.add(m);
                }

                result.put("labels", x);
                result.put("values", y);

                return AjaxResult.success(result);
            case "2":
                for (Map.Entry<String, List<EvaluationCbInfo>> entry : area.entrySet()) {
                    Map<String, Object> m = new HashMap<>();
                    m.put("name", entry.getKey());
                    m.put("type", "line");
                    ArrayList<String> d = new ArrayList<>();
                    for (String s : x) {
                        Map<String, List<EvaluationCbInfo>> map2 = entry.getValue().stream().collect(Collectors.groupingBy(EvaluationCbInfo::getPjjgsj, Collectors.toList()));
                        List<EvaluationCbInfo> infoList = map2.get(s);
                        if (CollUtil.isNotEmpty(infoList)) {
                            d.add(String.valueOf(infoList.size()));
                        } else {
                            d.add("-");
                        }
                    }

                    m.put("data", d);
                    y.add(m);
                }

                result.put("labels", x);
                result.put("values", y);

                return AjaxResult.success(result);
            case "3":
                for (Map.Entry<String, List<EvaluationCbInfo>> entry : area.entrySet()) {
                    Map<String, Object> m = new HashMap<>();
                    m.put("name", entry.getKey());
                    m.put("type", "line");
                    ArrayList<String> d = new ArrayList<>();
                    for (String s : x) {
                        Map<String, List<EvaluationCbInfo>> map2 = entry.getValue().stream().collect(Collectors.groupingBy(EvaluationCbInfo::getPjjgsj, Collectors.toList()));
                        List<EvaluationCbInfo> infoList = map2.get(s);
                        List<EcoCbfeeitem> cbFeeItems = new ArrayList<>();

                        if (CollUtil.isNotEmpty(infoList)) {
                            for (EvaluationCbInfo cleanInfo : infoList) {
                                if (StringUtils.isNotEmpty(cleanInfo.getDataJson())) {
                                    EcoCbfeeitem cbFeeItem = JSON.parseObject(cleanInfo.getDataJson(), EcoCbfeeitem.class);
                                    if (cbFeeItem != null) {
                                        cbFeeItems.add(cbFeeItem);
                                    }
                                }
                            }
                        }

                        if (CollUtil.isNotEmpty(cbFeeItems)) {

                            List<EcoCbfeeitem> yspriceList = cbFeeItems.stream().filter(e -> e.getYsprice() != null).collect(Collectors.toList());

                            BigDecimal ystotal = yspriceList.stream().map(EcoCbfeeitem::getYsprice).reduce(BigDecimal.ZERO, BigDecimal::add);

                            d.add(ystotal.toPlainString());
                        } else {
                            d.add("-");
                        }

                    }

                    m.put("data", d);
                    y.add(m);
                }

                result.put("labels", x);
                result.put("values", y);

                return AjaxResult.success(result);
            case "4":
                for (Map.Entry<String, List<EvaluationCbInfo>> entry : area.entrySet()) {
                    Map<String, Object> m = new HashMap<>();
                    m.put("name", entry.getKey());
                    m.put("type", "line");
                    ArrayList<String> d = new ArrayList<>();
                    for (String s : x) {
                        Map<String, List<EvaluationCbInfo>> map2 = entry.getValue().stream().collect(Collectors.groupingBy(EvaluationCbInfo::getPjjgsj, Collectors.toList()));
                        List<EvaluationCbInfo> infoList = map2.get(s);
                        List<EcoCbfeeitem> cbFeeItems = new ArrayList<>();

                        if (CollUtil.isNotEmpty(infoList)) {
                            for (EvaluationCbInfo cleanInfo : infoList) {
                                if (StringUtils.isNotEmpty(cleanInfo.getDataJson())) {
                                    EcoCbfeeitem cbFeeItem = JSON.parseObject(cleanInfo.getDataJson(), EcoCbfeeitem.class);
                                    if (cbFeeItem != null) {
                                        cbFeeItems.add(cbFeeItem);
                                    }
                                }
                            }
                        }

                        if (CollUtil.isNotEmpty(cbFeeItems)) {

                            List<EcoCbfeeitem> taxList = cbFeeItems.stream().filter(e -> e.getTargettaxprice() != null).collect(Collectors.toList());

                            BigDecimal tartotal = taxList.stream().map(EcoCbfeeitem::getTargettaxprice).reduce(BigDecimal.ZERO, BigDecimal::add);

                            d.add(tartotal.toPlainString());
                        } else {
                            d.add("-");
                        }

                    }

                    m.put("data", d);
                    y.add(m);
                }

                result.put("labels", x);
                result.put("values", y);

                return AjaxResult.success(result);
            case "5":
                for (Map.Entry<String, List<EvaluationCbInfo>> entry : area.entrySet()) {
                    Map<String, Object> m = new HashMap<>();
                    m.put("name", entry.getKey());
                    m.put("type", "line");
                    ArrayList<String> d = new ArrayList<>();
                    for (String s : x) {
                        Map<String, List<EvaluationCbInfo>> map2 = entry.getValue().stream().collect(Collectors.groupingBy(EvaluationCbInfo::getPjjgsj, Collectors.toList()));
                        List<EvaluationCbInfo> infoList = map2.get(s);
                        List<EcoCbfeeitem> cbFeeItems = new ArrayList<>();

                        if (CollUtil.isNotEmpty(infoList)) {
                            for (EvaluationCbInfo cleanInfo : infoList) {
                                if (StringUtils.isNotEmpty(cleanInfo.getDataJson())) {
                                    EcoCbfeeitem cbFeeItem = JSON.parseObject(cleanInfo.getDataJson(), EcoCbfeeitem.class);
                                    if (cbFeeItem != null) {
                                        cbFeeItems.add(cbFeeItem);
                                    }
                                }
                            }
                        }


                        if (CollUtil.isNotEmpty(cbFeeItems)) {

                            List<EcoCbfeeitem> yspriceList = cbFeeItems.stream().filter(e -> e.getYsprice() != null).collect(Collectors.toList());

                            BigDecimal ystotal = yspriceList.stream().map(EcoCbfeeitem::getYsprice).reduce(BigDecimal.ZERO, BigDecimal::add);

                            List<EcoCbfeeitem> taxList = cbFeeItems.stream().filter(e -> e.getTargettaxprice() != null).collect(Collectors.toList());

                            BigDecimal tartotal = taxList.stream().map(EcoCbfeeitem::getTargettaxprice).reduce(BigDecimal.ZERO, BigDecimal::add);

                            BigDecimal r = ystotal.subtract(tartotal).setScale(3, BigDecimal.ROUND_HALF_UP);

                            d.add(r.toPlainString());
                        } else {
                            d.add("-");
                        }
                    }

                    m.put("data", d);
                    y.add(m);
                }

                result.put("labels", x);
                result.put("values", y);

                return AjaxResult.success(result);
            case "6":
                for (Map.Entry<String, List<EvaluationCbInfo>> entry : area.entrySet()) {
                    Map<String, Object> m = new HashMap<>();
                    m.put("name", entry.getKey());
                    m.put("type", "line");
                    ArrayList<String> d = new ArrayList<>();
                    for (String s : x) {
                        Map<String, List<EvaluationCbInfo>> map2 = entry.getValue().stream().collect(Collectors.groupingBy(EvaluationCbInfo::getPjjgsj, Collectors.toList()));
                        List<EvaluationCbInfo> infoList = map2.get(s);
                        List<EcoCbfeeitem> cbFeeItems = new ArrayList<>();

                        if (CollUtil.isNotEmpty(infoList)) {
                            for (EvaluationCbInfo cleanInfo : infoList) {
                                if (StringUtils.isNotEmpty(cleanInfo.getDataJson())) {
                                    EcoCbfeeitem cbFeeItem = JSON.parseObject(cleanInfo.getDataJson(), EcoCbfeeitem.class);
                                    if (cbFeeItem != null) {
                                        cbFeeItems.add(cbFeeItem);
                                    }
                                }
                            }
                        }

                        if (CollUtil.isNotEmpty(cbFeeItems)) {

                            List<EcoCbfeeitem> yspriceList = cbFeeItems.stream().filter(e -> e.getYsprice() != null).collect(Collectors.toList());
                            List<EcoCbfeeitem> taxList = cbFeeItems.stream().filter(e -> e.getTargettaxprice() != null).collect(Collectors.toList());

                            BigDecimal ystotal = yspriceList.stream().map(EcoCbfeeitem::getYsprice).reduce(BigDecimal.ZERO, BigDecimal::add);
                            BigDecimal tartotal = taxList.stream().map(EcoCbfeeitem::getTargettaxprice).reduce(BigDecimal.ZERO, BigDecimal::add);

                            String s1 = "0";
                            if (BigDecimal.ZERO.compareTo(ystotal) != 0) {
                                s1 = ystotal.subtract(tartotal).divide(ystotal, 3, BigDecimal.ROUND_HALF_UP).toPlainString();
                            }
                            d.add(s1);
                        } else {
                            d.add("-");
                        }

                    }

                    m.put("data", d);
                    y.add(m);
                }

                result.put("labels", x);
                result.put("values", y);

                return AjaxResult.success(result);
            default:
                break;
        }
        return null;
    }


    @PostMapping("/initShowTableData")
    public AjaxResult initShowTableData(@RequestBody ZjfzckDto zjfzckDto) {
        String[] dateRange = zjfzckDto.getDateRange();
        if (dateRange != null && dateRange.length >= 2) {
            if (StringUtil.isNotNullOrEmpty(dateRange[0])) {
                zjfzckDto.setTimeStart(dateRange[0]);
            }

            if (StringUtil.isNotNullOrEmpty(dateRange[1])) {
                zjfzckDto.setTimeEnd(dateRange[1]);
            }
        }

        if ("全部".equals(zjfzckDto.getProjectDept())) {
            zjfzckDto.setProjectDept(null);
        }

        String address = StrUtil.join("", zjfzckDto.getAddressList());
        if ("全部".equals(address)) {
            zjfzckDto.setAddress(null);
        } else {
            zjfzckDto.setAddress(address);
        }

        if ("全部".equals(zjfzckDto.getPjjgsj())) {
            zjfzckDto.setPjjgsj(null);
        }

        String type = zjfzckDto.getType();
        if ("2".equals(type)) {
            String code = zjfzckDto.getListCode();
            Map<String, Object> map = new HashMap<>();
            map.put("code", code);
            map.put("pjjsj", zjfzckDto.getPjjgsj());
            map.put("xmgs", zjfzckDto.getProjectDept());
            map.put("region", zjfzckDto.getAddress());
            //借用了type属性后面需要重置
            zjfzckDto.setType("");
            List<EvaluationDataPrice> list = zjfZckService.findAllEvaluationDataPrice(zjfzckDto);

            Map<String, List<EvaluationDataPrice>> map1 = list.stream().collect(Collectors.groupingBy(EvaluationDataPrice::getPjjgsj, Collectors.toList()));
            Map<String, List<EvaluationDataPrice>> mapIdList = list.stream().collect(Collectors.groupingBy(EvaluationDataPrice::getId, Collectors.toList()));

            List<EvaluationCbInfo> userGS = zjfZckService.findAllEvaluationInfo(map1);
            userGS.stream().forEach(v -> {
                if (v.getProjectDept() == null) {
                    v.setProjectDept("");
                }
                if (v.getAddress() == null) {
                    v.setAddress("");
                }
            });

            Map<String, List<EvaluationCbInfo>> listMap = userGS.stream().filter(v -> v.getAddress() != null).collect(Collectors.groupingBy(EvaluationCbInfo::getAddress, Collectors.toList()));

            List<Map<String, Object>> result = new ArrayList<>();
            for (Map.Entry<String, List<EvaluationCbInfo>> entry : listMap.entrySet()) {
                Map<String, Object> m = new HashMap<>();
                String key = entry.getKey();
                List<EvaluationCbInfo> value = entry.getValue();
                Map<String, List<EvaluationCbInfo>> qcMap = value.stream().collect(Collectors.groupingBy(EvaluationCbInfo::getPjjgsj));
                m.put("name", key);
                for (Map.Entry<String, List<EvaluationCbInfo>> qc : qcMap.entrySet()) {
                    String qcKey = qc.getKey().replaceAll("\\.", "-");
                    List<EvaluationCbInfo> value1 = qc.getValue();

                    for (EvaluationCbInfo info : value1) {
                        List<EvaluationDataPrice> prices = mapIdList.get(info.getPid());
                        if (CollUtil.isNotEmpty(prices)) {
                            BigDecimal userPrice = prices.get(0).getUserPrice();
                            if (userPrice != null) {
                                info.setPrice(userPrice);
                            }
                        }
                    }

                    List<String> pjType = value1.stream().map(EvaluationCbInfo::getPjType).collect(Collectors.toList());
                    List<EvaluationDataPrice> priceList = map1.get(qcKey);
                    if (pjType.contains("01")) {
                        if (CollUtil.isNotEmpty(priceList)) {
                            BigDecimal userPrice = priceList.get(0).getUserPrice();
                            if (userPrice != null && CollUtil.isNotEmpty(value1)) {
                                m.put(qcKey + "价格", value1.stream().map(EvaluationCbInfo::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(3, BigDecimal.ROUND_HALF_UP));
                            } else {
                                m.put(qcKey + "价格", 0);
                            }
                        }
                        m.put(qcKey + "使用次数（排名）", value1.size());
                    }


                    List<EcoCbfeeitem> cbFeeItems = new ArrayList<>();

                    if (CollUtil.isNotEmpty(value1) && pjType.contains("02")) {
                        for (EvaluationCbInfo cleanInfo : value1) {
                            if (StringUtils.isNotEmpty(cleanInfo.getDataJson())) {
                                EcoCbfeeitem cbFeeItem = JSON.parseObject(cleanInfo.getDataJson(), EcoCbfeeitem.class);
                                if (cbFeeItem != null) {
                                    cbFeeItems.add(cbFeeItem);
                                }
                            }
                        }
                    }

                    if (CollUtil.isNotEmpty(cbFeeItems)) {

                        List<EcoCbfeeitem> yspriceList = cbFeeItems.stream().filter(e -> e.getYsprice() != null).collect(Collectors.toList());
                        List<EcoCbfeeitem> taxList = cbFeeItems.stream().filter(e -> e.getTargettaxprice() != null).collect(Collectors.toList());

                        BigDecimal ystotal = yspriceList.stream().map(EcoCbfeeitem::getYsprice).reduce(BigDecimal.ZERO, BigDecimal::add);
                        BigDecimal tartotal = taxList.stream().map(EcoCbfeeitem::getTargettaxprice).reduce(BigDecimal.ZERO, BigDecimal::add);
                        BigDecimal lirune = ystotal.subtract(tartotal).setScale(3, BigDecimal.ROUND_HALF_UP);
                        BigDecimal lrl = null;
                        if (lirune != null && ystotal != null) {
                            if (ystotal.doubleValue() == 0) lrl = new BigDecimal(0.00);
                            if (!(ystotal.doubleValue() == 0))
                                lrl = lirune.divide(ystotal, 2, BigDecimal.ROUND_HALF_UP);
                        }

                        m.put(qcKey + "收入合价（排名）", ystotal);
                        m.put(qcKey + "成本合价（排名）", tartotal);
                        m.put(qcKey + "利润额（排名）", lirune);
                        m.put(qcKey + "利润率（排名）", lrl);
                    }
                }
                result.add(m);
            }
            return AjaxResult.success(result);
        } else {
            List<EvaluationDataPrice> list = zjfZckService.findAllEvaluationDataPrice(zjfzckDto);
            Map<String, List<EvaluationDataPrice>> map1 = list.stream().collect(Collectors.groupingBy(EvaluationDataPrice::getPjjgsj, Collectors.toList()));
            Map<String, List<EvaluationDataPrice>> mapIdList = list.stream().collect(Collectors.groupingBy(EvaluationDataPrice::getId, Collectors.toList()));

            List<EvaluationCbInfo> userGS = zjfZckService.findAllEvaluationInfo(map1);
            userGS.stream().forEach(v -> {
                if (v.getProjectDept() == null) {
                    v.setProjectDept("");
                }
                if (v.getAddress() == null) {
                    v.setAddress("");
                }
            });

            Map<String, List<EvaluationCbInfo>> listMap = userGS.stream().filter(e -> e.getProjectDept() != null).collect(Collectors.groupingBy(EvaluationCbInfo::getProjectDept, Collectors.toList()));

            List<Map<String, Object>> result = new ArrayList<>();
            for (Map.Entry<String, List<EvaluationCbInfo>> entry : listMap.entrySet()) {
                Map<String, Object> m = new HashMap<>();
                String key = entry.getKey();
                List<EvaluationCbInfo> value = entry.getValue();
                Map<String, List<EvaluationCbInfo>> qcMap = value.stream().collect(Collectors.groupingBy(EvaluationCbInfo::getPjjgsj, Collectors.toList()));
                m.put("name", key);
                for (Map.Entry<String, List<EvaluationCbInfo>> qc : qcMap.entrySet()) {
                    String qcKey = qc.getKey().replaceAll("\\.", "-");
                    List<EvaluationCbInfo> value1 = qc.getValue();

                    for (EvaluationCbInfo info : value1) {
                        List<EvaluationDataPrice> prices = mapIdList.get(info.getPid());
                        if (CollUtil.isNotEmpty(prices)) {
                            BigDecimal userPrice = prices.get(0).getUserPrice();
                            if (userPrice != null) {
                                info.setPrice(userPrice);
                            }
                        }
                    }

                    List<String> pjType = value1.stream().filter(v -> v.getPjType() != null).map(EvaluationCbInfo::getPjType).collect(Collectors.toList());
                    List<EvaluationDataPrice> priceList = map1.get(qcKey);
                    if (pjType.contains("01")) {
                        if (CollUtil.isNotEmpty(priceList)) {
                            BigDecimal userPrice = priceList.get(0).getUserPrice();
                            if (userPrice != null && CollUtil.isNotEmpty(value1)) {
                                m.put(qcKey + "价格", value1.stream().map(EvaluationCbInfo::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(3, BigDecimal.ROUND_HALF_UP));
                            } else {
                                m.put(qcKey + "价格", 0);
                            }
                        }
                        m.put(qcKey + "使用次数（排名）", value1.size());
                    }


                    List<EcoCbfeeitem> cbFeeItems = new ArrayList<>();

                    if (CollUtil.isNotEmpty(value1) && pjType.contains("02")) {
                        for (EvaluationCbInfo cleanInfo : value1) {
                            if (StringUtils.isNotEmpty(cleanInfo.getDataJson())) {
                                EcoCbfeeitem cbFeeItem = JSON.parseObject(cleanInfo.getDataJson(), EcoCbfeeitem.class);
                                if (cbFeeItem != null) {
                                    cbFeeItems.add(cbFeeItem);
                                }
                            }
                        }
                    }

                    if (CollUtil.isNotEmpty(cbFeeItems)) {

                        List<EcoCbfeeitem> yspriceList = cbFeeItems.stream().filter(e -> e.getYsprice() != null).collect(Collectors.toList());
                        List<EcoCbfeeitem> taxList = cbFeeItems.stream().filter(e -> e.getTargettaxprice() != null).collect(Collectors.toList());

                        BigDecimal ystotal = yspriceList.stream().map(EcoCbfeeitem::getYsprice).reduce(BigDecimal.ZERO, BigDecimal::add);
                        BigDecimal tartotal = taxList.stream().map(EcoCbfeeitem::getTargettaxprice).reduce(BigDecimal.ZERO, BigDecimal::add);
                        BigDecimal lirune = ystotal.subtract(tartotal).setScale(3, BigDecimal.ROUND_HALF_UP);
                        BigDecimal lrl = null;
                        if (lirune != null && ystotal != null) {
                            if (ystotal.doubleValue() == 0) lrl = new BigDecimal(0.00);
                            if (!(ystotal.doubleValue() == 0))
                                lrl = lirune.divide(ystotal, 2, BigDecimal.ROUND_HALF_UP);
                        }

                        m.put(qcKey + "收入合价（排名）", ystotal);
                        m.put(qcKey + "成本合价（排名）", tartotal);
                        m.put(qcKey + "利润额（排名）", lirune);
                        m.put(qcKey + "利润率（排名）", lrl);
                    }
                }
                result.add(m);
            }
            return AjaxResult.success(result);
        }
    }


    @PostMapping("/initshowDetailTableData")
    public TableDataInfo initshowDetailTableData(@RequestBody ZjfzckDto zjfzckDto) {
        String[] dateRange = zjfzckDto.getDateRange();
        if (dateRange != null && dateRange.length >= 2) {
            if (StringUtil.isNotNullOrEmpty(dateRange[0])) {
                zjfzckDto.setTimeStart(dateRange[0]);
            }

            if (StringUtil.isNotNullOrEmpty(dateRange[1])) {
                zjfzckDto.setTimeEnd(dateRange[1]);
            }
        }

        if ("全部".equals(zjfzckDto.getProjectDept())) {
            zjfzckDto.setProjectDept(null);
        }

        String address = StrUtil.join("", zjfzckDto.getAddressList());
        if ("全部".equals(address)) {
            zjfzckDto.setAddress(null);
        } else {
            zjfzckDto.setAddress(address);
        }

        if ("全部".equals(zjfzckDto.getPjjgsj())) {
            zjfzckDto.setPjjgsj(null);
        }

        String code = zjfzckDto.getListCode();
        Map<String, Object> map = new HashMap<>();
        map.put("code", code);
        map.put("pjjsj", zjfzckDto.getPjjgsj());
        map.put("xmgs", zjfzckDto.getProjectDept());
        map.put("region", zjfzckDto.getAddress());
        List<EvaluationDataPrice> list = zjfZckService.findAllEvaluationDataPrice(zjfzckDto);

        List<EvaluationDataPrice> result = new ArrayList<>();

        Map<String, List<EvaluationDataPrice>> listMap = Optional.ofNullable(list)
                .map(List::stream)
                .orElseGet(Stream::empty)
                .collect(Collectors.groupingBy(EvaluationDataPrice::getPjjgsj, Collectors.toList()));

        if (CollUtil.isNotEmpty(listMap)) {
            for (Map.Entry<String, List<EvaluationDataPrice>> entry : listMap.entrySet()) {
                List<EvaluationDataPrice> priceList = entry.getValue();
                if (priceList.size() > 1) {
                    List<String> collect = Optional.ofNullable(priceList)
                            .map(List::stream)
                            .orElseGet(Stream::empty)
                            .map(EvaluationDataPrice::getPriceRange).collect(Collectors.toList());

                    if (CollUtil.isNotEmpty(collect)) {
                        List<BigDecimal> priList = new ArrayList<>();
                        for (String s : collect) {
                            if (StrUtil.isNotBlank(s)) {
                                String[] price = s.split("-");
                                for (String p1 : price) {
                                    priList.add(BigDecimal.valueOf(Double.valueOf(p1)));
                                }
                            }
                        }

                        if (CollUtil.isNotEmpty(priList)) {
                            String max = priList.stream().max(BigDecimal::compareTo).orElse(BigDecimal.ZERO).setScale(3, BigDecimal.ROUND_HALF_UP).toPlainString();
                            String min = priList.stream().min(BigDecimal::compareTo).orElse(BigDecimal.ZERO).setScale(3, BigDecimal.ROUND_HALF_UP).toPlainString();
                            priceList.get(0).setPriceRange(min + "-" + max);
                            result.add(priceList.get(0));
                        }
                    }

                } else {
                    result.addAll(priceList);
                }
            }
        }
        return getDataTable(result);
    }


    @PostMapping("/getTableHeader")
    public AjaxResult getTableHeader(@RequestBody ZjfzckDto zjfzckDto) {
        Map<String, String> result = zjfZckService.findByCode(zjfzckDto);
        return AjaxResult.success(result);
    }


    @PostMapping("/getQccb")
    public AjaxResult getQccb(@RequestBody ZjfzckDto zjfzckDto) {
        String[] dateRange = zjfzckDto.getDateRange();
        if (dateRange != null && dateRange.length >= 2) {
            if (StringUtil.isNotNullOrEmpty(dateRange[0])) {
                zjfzckDto.setTimeStart(dateRange[0]);
            }

            if (StringUtil.isNotNullOrEmpty(dateRange[1])) {
                zjfzckDto.setTimeEnd(dateRange[1]);
            }
        }

        if ("全部".equals(zjfzckDto.getProjectDept())) {
            zjfzckDto.setProjectDept(null);
        }

        String address = StrUtil.join("", zjfzckDto.getAddressList());
        if ("全部".equals(address)) {
            zjfzckDto.setAddress(null);
        } else {
            zjfzckDto.setAddress(address);
        }

        if ("全部".equals(zjfzckDto.getPjjgsj())) {
            zjfzckDto.setPjjgsj(null);
        }
        List<EvaluationResults> result = zjfZckService.getQc(zjfzckDto);
        return AjaxResult.success(result);
    }
}
